package structures.pane;



import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import analyzers.InputAnalyzer;

import constants.Globals;

import structures.xmltable.ServiceParameterTable;
import util.GlobalUtils;
import util.HibernateUtil;
import xml.sax.esi.EsiXMLProcessor;
import xml.sax.plm.PlmInXMLProcessor;
import domain.PaneNodeDAO;
import domain.ProjectInfoDAO;
import exception.ExceptionStringHandler;
import exception.IBToolsException;

public class XmlPaneNode implements java.io.Serializable, Globals{
	
	private static final long serialVersionUID = 8822624614042781302L;
	
	private int paneIndex;
	private PaneNodeDAO paneNode;
	private boolean selectedToCompare;
	
	private XmlPane containerPane;
	private String outXml;
	private String xmlInfo;
	
	private String nodeFromOtherSource;

	private boolean longNodeName;
	
	XmlPaneNode(XmlPane containerPane, String xmlString, long projectOid, String serviceName, int paneIndex) throws IBToolsException{
		PaneNodeDAO xmlData = new PaneNodeDAO(new ProjectInfoDAO(projectOid), xmlString, true, serviceName, null);
		setXmlPaneNode(xmlData);
		this.paneIndex = paneIndex;
		this.containerPane = containerPane;
	}
	XmlPaneNode(XmlPane containerPane, PaneNodeDAO xmlData, int paneIndex) throws IBToolsException{
		setXmlPaneNode(xmlData);
		this.paneIndex = paneIndex;
		this.containerPane = containerPane;
	}
	
	private void setXmlPaneNode(PaneNodeDAO xmlData) throws IBToolsException {
		if(
			(xmlData != null && xmlData.getProject().getProjectCode() != null 
					&& !xmlData.getProject().getProjectCode().equals(EMPTY_STRING))
				||
			(xmlData != null && xmlData.getXmlString() != null 
					&& !xmlData.getXmlString().equals(Globals.EMPTY_STRING))
				){
			this.paneNode = xmlData;
			if(xmlData.getStatus()){
				this.paneNode.setStatus(true);
			}
		}
		else {
			throw new IBToolsException(new ExceptionStringHandler(true, "Eksik XML pano icerigi kaydedilemez"));
		}
	}
	
	public String getXmlstring(){
		String xmlstring = null;
		if(paneNode != null)
			xmlstring = paneNode.getXmlString();
		else
			xmlstring = "";
		return xmlstring;
	}

	public ServiceParameterTable getAsTable() throws IBToolsException {
		return InputAnalyzer.getAsTable(paneNode.getXmlString(), true);
	}
	public boolean isDeleteButtonVisible(){
		return paneNode.getOid() != null && paneNode.getOid() > 0 && paneNode.getStatus();
	}
	public boolean isSaveButtonVisible(){
		boolean visible = paneNode.getOid() == null || paneNode.getOid() < 1 || !paneNode.getStatus();
		visible = visible ? visible : containerPane.getSelectedToUpdateIndex() == this.paneIndex;
		return visible;
	}
	public boolean isCloseButtonVisible(){
		return paneNode == null || paneNode.getOid() == null || paneNode.getOid() < 1;
	}
	public boolean isPersisted() {
		return paneNode.getOid() != null && paneNode.getOid() > 0;
	}
	public String getSessionState() {
		String updating = "";
		if( !containerPane.isInPane()){
			if(selectedToCompare)
				updating = " node-state-updating";
//				return "updating";
			return "outnode" + updating;
		}
		if(paneNode != null){
			if(containerPane.getSelectedToUpdateIndex() == this.paneIndex){
				updating = " node-state-updating";
			}
			if(nodeFromOtherSource != null){
				return nodeFromOtherSource + updating;
			}
			if(paneNode.getOid() != null && paneNode.getOid() > 0){
				if(paneNode.getStatus())
					return "persisted" + updating;
				else
					return "deleted" + updating;
			}
		}
		return "new" + updating;
	}
	private void persist() throws IBToolsException{
    	Session session = HibernateUtil.getDEVSession();
        Transaction tx = null;
        
        try {
			tx = HibernateUtil.beginDEVTransaction(session);
			if(paneNode.getOid() != null)
				session.merge(paneNode);
			else
				session.saveOrUpdate(paneNode);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		}
	}

	public String getVisualServiceName(int size){
		String text = getDisplayText();
//		String name = getPaneOrder() + " - ";
		String name = "";
		longNodeName = text.length() > size;
		size -= 2;
//		if(SessionManager.isAdminUser()){
//			name = getPaneOrder() == null ? "" : getPaneOrder() + "-";
//		}
		if(text != null){
			if(text.length() > size){
//				if(SessionManager.isAdminUser())
//					name += getServiceName().substring(0, size - 3);
//				else
					name += text.substring(0, size);
//				name += "...";
			}
			else {
				name += text;
			}
		}
		return name;
	}
	public String getDisplayText(){
		String displayText = null;
		if(paneNode.getDescription() == null || paneNode.getDescription().equalsIgnoreCase(EMPTY_STRING)){
			displayText = paneNode.getServiceName();
		}
		else {
			displayText = paneNode.getDescription();
		}
		return displayText;
	}
	
	void evict() throws IBToolsException{
    	Session session = HibernateUtil.getDEVSession();
    	Transaction tx = null;
    	try {
			tx = HibernateUtil.beginDEVTransaction(session);
	        session.evict(paneNode);
			tx.commit();
			
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		}
	}
	void activate(String xmlString, String serviceName, long projectOid, String description) throws IBToolsException{
		paneNode.setStatus(true);
		if(xmlString != null){
			paneNode.setXmlString(xmlString.trim());
			paneNode.setServiceName(serviceName);
			paneNode.setProject(new ProjectInfoDAO(projectOid));
		}
		if(description != null)
			paneNode.setDescription(description);
		persist();
	}
	void delete() throws IBToolsException{
		paneNode.setStatus(false);
		persist();
		evict();
		
		paneNode.setStatus(true);
		paneNode.setOid(null);
	}
	void setOrder(int order){
		paneNode.setPaneIndex(new Integer(order));
	}
	public void SetDescription(String description){
		paneNode.setDescription(description);
	}
	public boolean nodeIsSelectedToUpdate(){
		return containerPane.getSelectedToUpdateIndex() == this.paneIndex;
	}
	public boolean isSelectedToCompare() {
		return selectedToCompare;
	}
	public void setSelectedToCompare(boolean selectedToCompare) {
		this.selectedToCompare = selectedToCompare;
	}
	public int getPaneIndex() {
		return paneIndex;
	}
	public void setPaneIndex(int paneIndex) {
		this.paneIndex = paneIndex;
	}
	public String getOutXml() {
		return outXml;
	}
	public void setOutXml(String outXml) {
		this.outXml = outXml;
	}
	
	public String getNodeFromOtherSource() {
		return nodeFromOtherSource;
	}
	public void setNodeFromOtherSource(String nodeFromOtherSource) {
		this.nodeFromOtherSource = nodeFromOtherSource;
	}

	public String getSourceClass() {
		return nodeFromOtherSource;
	}
	public boolean isContainsOutput(){
		return outXml != null;
	}
	public Integer getPaneOrder(){
		return paneNode.getPaneIndex();
	}
	public String getXmlInfo() {
		return xmlInfo;
	}
	public void setXmlInfo(String xmlInfo) {
		this.xmlInfo = xmlInfo;
	}
	public boolean isLongName(){
		boolean isLong = false;
		String text = getDisplayText();
		if( text!= null){
			isLong = text.length() > 50;
		}
		return isLong;
	}
	public boolean isLongNodeName() {
		return longNodeName;
	}
	public void setLongNodeName(boolean longNodeName) {
		this.longNodeName = longNodeName;
	}
	
}
